<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class UploadController extends Controller {

	public function upload(Request $request) {
		$file = $request->file('file');
		if ($file->isValid()) {
			$ext = $file->getClientOriginalExtension();
			$real_path = $file->getRealPath();
			$type = $file->getClientMimeType();
			$file_name = date('Y-m-d-H-i-s') . '-' . uniqid() . '.' . $ext;
			$bool = Storage::disk('uploads')->put($file_name, file_get_contents($real_path));
			if (!$bool) {
				abort(500, 'file move fail');
			}
			$file_path = asset('storage/uploads/' . $file_name);
			return ['code' => 0, 'data' => $file_path];
		} else {
			abort(422, 'file upload fail');
		}
	}
}
